package com.mutouren.modules.datapush;

import javax.annotation.Resource;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.modules.datapush.archives.Reciever;
import com.mutouren.modules.datapush.archives.Sender;

@Service
public class ServerManager implements InitializingBean, DisposableBean {
	
	@Resource
	private Reciever reciever;
	@Resource
	private Sender sender;
	
	private final static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	private final static Logger runLogger = LogManager.getLogger(LogAlias.SystemRun.name());	
	
	public ServerManager() {
		//System.out.println("hello");
	}
	
	public void open() {
		try {
			runLogger.info("ServerManager open begin");
			reciever.open();
			sender.open();
			runLogger.info("ServerManager open end");
		} catch(Throwable t) {
			errorLogger.error("ServerManager open error", t);
			throw t;
		}
	}
	
	public void close() {
		try {
			runLogger.info("ServerManager close begin");
			reciever.close();
			sender.close();
			runLogger.info("ServerManager close end");
		} catch(Throwable t) {
			errorLogger.error("ServerManager close error", t);
		}
	}

	@Override
	public void destroy() throws Exception {
		close();		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		open();
	}
}
